package com.huanzhidadi.single.completablefuture;

import java.util.concurrent.*;

/**
 * 返回异步线程执行之后的结果
 * <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
 */
public class SupplyAsyncTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        System.out.println("主程序：" + Thread.currentThread().getName());
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子程序：" + Thread.currentThread().getName());
            return "1234";
        },executorService);
        String s = future.get();
        System.out.println(s);
        System.out.println("主程序运行结束");
        executorService.shutdown();
    }
}
